package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Practice;
import com.wxxymaker.edu.service.PracticeService;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/10/15 0:33
 * @DESCRIBE 练习
 */
@Path(value = "/course/practice/api", restful = true)
public class PracticeHandlerApi {

    @Inject
    private PracticeService practiceService;

    /**
     * 按课程任务进行分组查询
     * @param kid 课程id
     */
    @PostRoute("/:kid/groupTask")
    public RestResponse groupTask(@PathParam Integer kid,
                                  @Param(defaultValue = "1") Integer page,
                                  @Param(defaultValue = "10") Integer size) {
        Page<Practice> practicePage = practiceService.pageGroup(kid, page, size);
        if (practicePage!=null){
            return RestResponse.ok(practicePage);
        }
        return RestResponse.fail();
    }

    @PostRoute("/one/:id")
    public RestResponse one(@PathParam Integer id) {
        Practice one = practiceService.findOne(id, true);
        if (one != null) {
            return RestResponse.ok(one);
        }
        return RestResponse.fail();
    }

    @PostRoute("/:kid/pageTask/:tid")
    public RestResponse page(@PathParam Integer kid,
                             @PathParam Integer tid,
                             @Param(defaultValue = "1") Integer page,
                             @Param(defaultValue = "10") Integer size) {
        Page<Practice> page1 = practiceService.page(kid, tid, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    @PostRoute("/:kid/page")
    public RestResponse page(@PathParam Integer kid,
                             @Param(defaultValue = "0") Integer tid,
                             @Param(defaultValue = "-1") Integer type,
                             @Param(defaultValue = "") String key,
                             @Param(defaultValue = "1") Integer page,
                             @Param(defaultValue = "10") Integer size) {
        Page<Practice> page1 = practiceService.page(kid, tid, type, key, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }
}
